package longrise.phone.com.bjjt_henan.compensate;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.LFView;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerCar;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.PrefUtils;
import com.longrise.bjjt.util.UiUtil;

import longrise.phone.com.bjjt_henan.Constant.MsgConstant;
import longrise.phone.com.bjjt_henan.Constant.URLConstant;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.main.NewMainView;
import longrise.phone.com.bjjt_henan.myaccident.AccidentMainView;

/**
 * 服务评价 ，Created by Administrator on 2016/5/12.
 */

public class ServiceeValuateView extends LFView implements View.OnClickListener {
    private Context context = null;
    private View view = null;
    private View btn_promptafterbankcardinfo;
    private RadioGroup main_bottom_rg;
    private RadioButton main_rb_verygood;
    private RadioButton main_rb_good;
    private RadioButton main_rb_simple;
    private RadioButton main_rb_bad;
    private Button btn_serviceevaluate;
    private EditText et_serviceevaluate;
    private Dialog processDialog;
    private String reaonCode;
    private String appCaseno;
    private String carNo;
    private BaseApplication mApplication;
    private String personescord;
    private LinearLayout hn_btn_back;
    private TextView tv_title;

    public ServiceeValuateView(Context c, String appCaseno, String carNo) {
        super(c);
        this.context = c;
        this.appCaseno = appCaseno;
        this.carNo = carNo;
        this.mApplication = (BaseApplication) c.getApplicationContext();
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public void refresh() {

    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        initView();
        regEvent(true);
    }

    private void initView() {
        view = View.inflate(context, R.layout.view_serviceevaluate, null);
        btn_serviceevaluate = (Button) view.findViewById(R.id.btn_serviceevaluate);
        et_serviceevaluate = (EditText) view.findViewById(R.id.et_serviceevaluate);
        main_bottom_rg = (RadioGroup) view.findViewById(R.id.main_bottom_rg);
        main_rb_verygood = (RadioButton) view.findViewById(R.id.main_rb_verygood);
        main_rb_good = (RadioButton) view.findViewById(R.id.main_rb_good);
        main_rb_simple = (RadioButton) view.findViewById(R.id.main_rb_simple);
        main_rb_bad = (RadioButton) view.findViewById(R.id.main_rb_bad);
        hn_btn_back = (LinearLayout) view.findViewById(R.id.hn_btn_back);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText("服务评价");
    }

    private void regEvent(boolean b) {
        if (null != btn_serviceevaluate) {
            btn_serviceevaluate.setOnClickListener(b ? this : null);
        }
        if (null != main_bottom_rg) {
            main_bottom_rg.setOnCheckedChangeListener(occl);
            ((RadioButton) main_bottom_rg.getChildAt(0)).setChecked(true);
        }

        if (null != hn_btn_back) {
            hn_btn_back.setOnClickListener(b ? this : null);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_serviceevaluate:
//                String trim = et_serviceevaluate.getText().toString().trim();
//                if (TextUtils.isEmpty(trim)) {//用户没有输入意见或建议，弹框
//                    UiUtil.showToast(context, "请输入意见或建议");
//                } else {
                submitserviceevaluate();
//                }
                break;
            case R.id.hn_btn_back:
                closeForm();
                OnDestroy();
                break;
        }
    }

    private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        final String[] reasons = {"定损时间较长", "理赔时间较长", "系统响应时间较长", "系统人性化不够", "一般（其他）"};
        builder.setItems(reasons, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //reaonCode = reasons[which];
                reaonCode = which + "";
            }
        });
        builder.show();
    }


    private OnCheckedChangeListener occl = new OnCheckedChangeListener() {
        // 当radioGroup里的RadioButton勾选状态变化了，会调用此方法
        // group 哪个RadioGroup设置了监听
        // checkedId RadioGroup下的孩子RadioButton,被勾选了，它的id
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            int index = group.indexOfChild(group.findViewById(checkedId));
            switch (checkedId) {
                case R.id.main_rb_verygood:
                    //提交满意信息
                    // personescord="非常满意";
                    personescord = "0";
                    break;
                case R.id.main_rb_good:
                    personescord = "1";
                    break;
                case R.id.main_rb_simple:
                    personescord = "2";
                    showDialog();
                    break;
                case R.id.main_rb_bad:
                    personescord = "3";
                    showDialog();
                    break;
            }
        }


    };

    /**
     * 提交服务评价
     */
    private void submitserviceevaluate() {
        EntityBean userinfo = mApplication.getLoginInfo().getBean("userinfo");
        EntityBean bean = new EntityBean();
        bean.put("appcaseno", appCaseno);//移动报案号
        bean.put("casecarno", carNo);//车牌号
        bean.put("casetelephoe", userinfo.get("mobilephone"));//手机号
        bean.put("personevaluate", et_serviceevaluate.getText().toString().trim());//意见与建议
        bean.put("personescord", personescord);//评价

        bean.put("reasoncode", reaonCode);//不满意代码表
        processDialog = UiUtil.showProcessDialog(context, "加载数据中...");
        LoadDataManagerCar.getInstance(context).callService(null, mApplication.getServerUrl(),
                URLConstant.KCKPSERVICEEVALUATION, bean, new LoadDataManagerFather.OnRequestCompleteListener() {
                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        if (null != processDialog) {
                            if (processDialog.isShowing()) {
                                processDialog.dismiss();
                            }
                        }
                        EntityBean bean = (EntityBean) result;
                        String restate = bean.getString("restate");
                        if ("1".equals(restate)) {
                            UiUtil.showToast(context, "评价成功");
                            //回到主页
                            backHomeView();
                            return;
                        } else {
                            UiUtil.showToast(context, "评价失败");
                        }
                    }

                    @Override
                    public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                        if (null != processDialog) {
                            if (processDialog.isShowing()) {
                                processDialog.dismiss();
                            }
                        }
                        if (resultType == LoadDataManagerFather.ResultType.Failure) {
                            UiUtil.showToast(context, "连接超时，请检查网络后重试。");
                        } else if (resultType == LoadDataManagerFather.ResultType.NoNetWorks) {
                            UiUtil.showToast(context, "无网络连接，请检查您的网络");
                        }
                    }

                    @Override
                    public void onFinished(String key, String service) {
                        if (null != processDialog) {
                            if (processDialog.isShowing()) {
                                processDialog.dismiss();
                            }
                        }
                    }
                });
    }

    /**
     * 案件处理结束，回到主界面
     */
    private void backHomeView() {
        LFView view = null;
        boolean isHistroy = PrefUtils.getBoolean(context, MsgConstant.HISTORY_CASE, false);
        if (isHistroy) {//回到历史案件列表
            OnDestroy();
            closeForm();
            FrameworkManager.getInstance().closePopForm();
            view = new AccidentMainView(context);
        } else {
            FrameworkManager.getInstance().destroyAllForm();
            view = new NewMainView(context);
        }
        FrameworkManager.getInstance().showNewForm(context, view);
    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
    }
}
